/*
 * QTXpost - a Qt-based blog client.
 * Copyright (C) 2010 Helen Sommer
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program (in COPYING); if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA  02111-1307  USA
 *
 * The project is hosted on http://code.google.com/p/qtxpost
 */

#include "GroupsTree.h"
#include "MainWindow.h"

extern MainWindow *root;

GroupsTree::GroupsTree(QWidget *parent)
    : QTreeWidget(parent)
{
  QHBoxLayout *l = new QHBoxLayout(this);
  setAcceptDrops(true);
  setHeaderLabel(tr("Groups"));
}

void GroupsTree::stopSignals()
{
  disconnect(this);
}

void GroupsTree::startSignals()
{
  connect(this, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(updateItem(QTreeWidgetItem *)));
}

QStringList GroupsTree::mimeTypes () const 
{
  QStringList list; // list of accepted mime types for drop
  list.append("text/plain");
  return list;
}

bool GroupsTree::dropMimeData(QTreeWidgetItem *parent, int index, const QMimeData *data, Qt::DropAction action)
{
  QTreeWidgetItem *item;
  QString text = data->text();

  if (parent == 0) return true; // can't insert user as a new group
  if (parent->parent() != 0) {
    //can't insert user as a child of another user - insert into containing group instead
    parent = parent->parent();
  }
  root->friendsManager->processDropData(parent->text(0), text);
  return true;  
}

void GroupsTree::keyPressEvent(QKeyEvent *event)
{
  if (event->key() == Qt::Key_Delete) {
    QList<QTreeWidgetItem *> selected = this->selectedItems();
    for (int i = 0; i < selected.count(); i++) {
      QTreeWidgetItem *parent = selected.at(i)->parent();
      if (parent == 0) { // it is a group
        QMessageBox msgBox(root->friendsManager);
        QString groupName = ((GroupTreeWidgetItem *)selected.at(i))->name;
        QString message = QString(tr("Really delete group %1?")).arg(groupName);
        msgBox.setText(message);
        msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
        msgBox.setDefaultButton(QMessageBox::No);
        if (msgBox.exec() == QMessageBox::Yes) {
          root->friendsManager->deleteGroupStart((GroupTreeWidgetItem *)selected.at(i));
        }
      }
      else {
        QString groupName = parent->text(0);
        QString userName = selected.at(i)->text(0);
        root->friendsManager->deleteUserGroupStart(groupName, userName, selected.at(i));
      }
      break;
    }
    event->accept();
  }
  else if (event->key() == Qt::Key_Return) {
    event->accept();
  }
  else {
    QTreeWidget::keyPressEvent(event);
  }
}


void GroupsTree::mouseReleaseEvent(QMouseEvent *event)
{
  if (event->button() == Qt::RightButton) {
    QList<QTreeWidgetItem *> selected = this->selectedItems();
    for (int i = 0; i < selected.count(); i++) {
      QTreeWidgetItem *parent = selected.at(i)->parent();
      if (parent != 0) return; // process only groups, not users
      QString groupName = ((GroupTreeWidgetItem *)selected.at(i))->name;
      root->friendsManager->editGroup->initialize((GroupTreeWidgetItem *)selected.at(i));
    }
  }
  QTreeWidget::mouseReleaseEvent(event);
}

void GroupsTree::updateItem(QTreeWidgetItem *item)
{
  QString text = item->text(0);
  if (item->parent() != 0) return; // user name: can't edit
  
  if (text == "") {
    // the callback will be called again but it's better than allowing a group with empty name
    item->setText(0, ((GroupTreeWidgetItem *)item)->name); 
    return;
  }

  root->friendsManager->updateGroupStart((GroupTreeWidgetItem *)item, -1, -1);
}



